﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Windows;
using Caliburn.Micro;
using Microsoft.Phone.Tasks;
using Wulong.Phone.Qiushi.i18n;
using Wulong.Phone.Qiushi.Models;

namespace Wulong.Phone.Qiushi.ViewModels
{
    public class CommentsViewModel : 
        BusyIndicatorViewModelBase
    {
        private const string ShowPostIcon = "/images/expand.png";
        private const string HidePostIcon = "/images/collapse.png";
        private static readonly string ShowPostText = AppResources.V_Comments_ShowPost;
        private static readonly string HidePostText = AppResources.V_Comments_HidePost;

        public CommentsViewModel(
            INavigationService navigationService,
            IEventAggregator events)
        {
            _navigationService = navigationService;
            _events = events;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            PostButtonIcon = ShowPostIcon;
            PostButtonLabel = ShowPostText;
            if(_comments.Count==0)
            {
                LoadComments();
            }
        }
        public void LoadComments()
        {
            IsBusy = true;
            _subject = QiushiMapper.GetObservableComment(Id)
                .ObserveOnDispatcher()
                .Finally(() =>
                {
                    IsBusy = false;
                })
                .Subscribe(_comments.Add, err=> MessageBox.Show(AppResources.Ex_Network_Down_Message + "\r\n" + err.Message,
                                                                AppResources.Ex_Error_Title,
                                                                MessageBoxButton.OK));
        }

        private string _qiushiContent;
        public string QiushiContent
        {
            get { return _qiushiContent; }
            set
            {
                _qiushiContent = value;
                NotifyOfPropertyChange(() => QiushiContent);
            }
        }

        private string _imageUrl;
        public string ImageUrl
        {
            get { return _imageUrl; }
            set
            {
                _imageUrl = value;
                NotifyOfPropertyChange(() => ImageUrl);
            }
        }

        private string _postButtonIcon;
        public string PostButtonIcon
        {
            get { return _postButtonIcon; }
            set
            {
                _postButtonIcon = value;
                NotifyOfPropertyChange(() => PostButtonIcon);
            }
        }

        private string _postButtonLabel;
        public string PostButtonLabel
        {
            get { return _postButtonLabel; }
            set
            {
                _postButtonLabel = value;
                NotifyOfPropertyChange(() => PostButtonLabel);
            }
        }

        public int Id { get; set; }
      
        public ObservableCollection<QiushiComment> _comments = new ObservableCollection<QiushiComment>();
        
        public IEnumerable<QiushiComment> Comments
        {
            get { return _comments; }
        }

        private bool _isPostVisible;
        public bool IsPostVisible
        {
            get { return _isPostVisible; }
            set 
            { 
                _isPostVisible = value;
                NotifyOfPropertyChange(() => IsPostVisible);
            }
        }
        public void TogglePostVisibility()
        {
            if (PostButtonIcon.Equals(ShowPostIcon))
            {
                PostButtonIcon = HidePostIcon;
                PostButtonLabel = HidePostText;
                IsPostVisible = true;
            }
            else
            {
                PostButtonIcon = ShowPostIcon;
                PostButtonLabel = ShowPostText;
                IsPostVisible = false;
            }
        }

        public void GoBack()
        {
            _navigationService.GoBack();
        }

        public void SentByEmail()
        {
            _events.RequestTask<EmailComposeTask>(x =>
            {
                x.Body = QiushiContent + ImageUrl +
                         AppResources.App_Email_Share_Signature;
                x.Subject = AppResources.App_Email_Share_Title;
            });
        }

        public void SentBySms()
        {
            _events.RequestTask<SmsComposeTask>(x => { x.Body = QiushiContent + "\r\n"+ ImageUrl + AppResources.App_Email_Share_Signature; });
        }
    }
}
